package com.openatc.agent.service.impl;

import com.openatc.agent.service.GenericMsgStrategy;
import com.openatc.agent.utils.RedisTemplateUtil;
import com.openatc.comm.data.MessageData;
import com.openatc.core.model.InnerError;
import com.openatc.core.model.RESTRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.openatc.core.common.IErrorEnumImplInner.E_110;

/**
 * @Classname GetStatusPattern
 * @Description 处理平台发送的方案状态请求
 * @Date 2022/7/19 13:41
 * @Created by panleilei
 */
@Service(value = "status/pattern")
public class StatusPatternRequest extends GenericMsgStrategy {
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    private String keyType = "response:status/pattern:";


    @Override
    public RESTRet preProcess(MessageData requestData) {
        // 1、直接向设备请求

        // 2、请求超时/上次请求未结束，从缓存中获取

        // 3、获取成功，更新redis中的数据

//        String agentid = requestData.getAgentid();
//        String key = keyType + agentid;
//        String value = redisTemplateUtil.getValue(key);
//        if (value == null) {
//            // 缓存中没有数据，向设备请求
//            return RESTRetUtils.successObj(null);
//        }
//
//        MessageData responseData;
//        try {
//            responseData = gson.fromJson(value, MessageData.class);
//        } catch (JsonSyntaxException e){
//            InnerError innerError = RESTRetUtils.innerErrorObj(agentid, E_104, requestData);
//            return RESTRetUtils.errorDetialObj(E_4002,innerError);
//        }
//
//        Long currentTime = System.currentTimeMillis();
//        Long createTime = Long.valueOf(responseData.getCreatetime());
//
//        // redis中数据时间和当前时间相差大于700ms，向设备请求
//        if (currentTime - createTime < 700) {
//            responseData.setSource(dataSource);
//            return RESTRetUtils.successObj(responseData);
//        }
        return super.preProcess(requestData);
    }


    /**
     * 信号机成功应答请求
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet successGetProcess(MessageData requestData, RESTRet responseData) {
        String agentid = requestData.getAgentid();
        MessageData res = (MessageData) responseData.getData();
        // 保存到redis
        String key = keyType + agentid;
        res.setCreatetime(String.valueOf(System.currentTimeMillis()));
        redisTemplateUtil.setValue(key,gson.toJson(res));
        return super.successGetProcess(requestData,responseData);
    }


    /** 信号机应答失败
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet failureGetProcess(MessageData requestData, RESTRet responseData) {

        InnerError innerError = (InnerError) responseData.getData();
        // 上次请求未结束/请求超时,从缓存中获取数据
        if (innerError.getErrorCode().equals(E_110.getErrorCode()) ) {

            RESTRet rest = redisTemplateUtil.getCacheData(requestData);
            return rest;
        }

        return super.failureGetProcess(requestData, responseData);
    }
}
